package com.faya.green.controller.test;

import com.faya.green.abnormal.ControllerException;
import com.faya.green.dao.test.TestDao;
import com.faya.green.model.test.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 用户 ：LX
 * 创建时间： 2018/7/5. 18:30
 * 地点：广州
 * 目的:
 * 结果：
 */
@Controller
@RequestMapping("/test")
public class HelloControllerTwo {

    @Autowired
    private TestDao testDao;

    /**
     * 测试mybatis的注解形式的查询是否可以，---ok
     * @return
     */
    @RequestMapping(value = "/findId.php")
    public String findId(ModelMap modelMap){
        String id = "0de5eb5d-5c16-44d6-926a-1c0d491dacb9";
        Test test = testDao.findById(id);
        modelMap.put("test", test);
        return "/test/hello";
    }

    /**
     * 测试异常  ---ok
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/testError")
    public String testError(ModelMap modelMap){
        String id = "0de5eb5d-5c16-44d6-926a-1c0d491dacb9";
        Test test = testDao.findById(id);
        modelMap.put("test", test);
        int i = 10 / 0;
        return "/test/hello";
    }

    /**
     * 测试自定义异常  ---ok
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/testCustomError")
    public String testCustomError(ModelMap modelMap) throws ControllerException {

        try {
            int i = 10 / 0;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ControllerException("除数不能为0");
        }
        return "/test/hello";
    }

}
